package cn.blog.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.blog.common.LayuiTableResult;
import cn.blog.common.PublicResultJosn;
import cn.blog.dto.LoginUser;
import cn.blog.dto.SystemUserAdd;
import cn.blog.entity.SystemUser;
import cn.blog.service.SystemUserService;
import cn.blog.utils.RefreshUserInfo;
import cn.blog.utils.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 后台用户接口
 * 
 * @author 偶尔有点困
 * @date 2018年5月4日
 */
@Api(tags = "后台用户")
@RequestMapping("/sysUser")
@RestController
public class SystemUserController {

	@Autowired
	private SystemUserService systemUserService;

	@Autowired
	private BCryptPasswordEncoder passwordEncoder;

	@Autowired
	private RefreshUserInfo refreshUserInfo;

	@PostMapping
	@ApiOperation(value = "添加用户", notes = "添加用户")
	@PreAuthorize("hasAuthority('sys:admin:add')")
	public PublicResultJosn add(@RequestBody SystemUserAdd user_add) {
		PublicResultJosn resultJosn = null;
		resultJosn = systemUserService.add(user_add);
		return resultJosn;
	}

	@DeleteMapping
	@ApiOperation(value = "单、批量删除用户")
	@PreAuthorize("hasAuthority('sys:admin:del')")
	public PublicResultJosn delete(@RequestBody List<String> deleteids) {
		PublicResultJosn resultJosn = null;
		resultJosn = systemUserService.delete(deleteids);
		return resultJosn;
	}

	@PutMapping
	@ApiOperation(value = "更新用户信息")
	@PreAuthorize("hasAuthority('sys:admin:update')")
	public PublicResultJosn update(@RequestBody SystemUserAdd user) {
		PublicResultJosn resultJosn = null;
		resultJosn = systemUserService.updateByExample(user);
		return resultJosn;
	}

	@GetMapping
	@ApiOperation(value = "用户列表条件模糊查询")
	@PreAuthorize("hasAnyAuthority('sys:admin:query','sys:admin:update')")
	public LayuiTableResult selectByExample(SystemUser user, @RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "10") Integer pageSize) {
		LayuiTableResult resultJosn = systemUserService.selectByExample(user, page, pageSize);
		return resultJosn;
	}

	@GetMapping(value = "/{uid}")
	@ApiOperation(value = "根据uid查找、包括所拥有的角色")
	@PreAuthorize("hasAnyAuthority('sys:admin:query','sys:admin:update')")
	public PublicResultJosn selectByUid(@PathVariable String uid) {
		PublicResultJosn resultJosn = systemUserService.selectByUid(uid);
		return resultJosn;
	}

	@GetMapping(value = "/current")
	@ApiOperation(value = "获取当前登录用户")
	public PublicResultJosn getCurrentUser() {
		return new PublicResultJosn(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), UserUtil.getCurrentUser());
	}

	@PutMapping(value = "/current")
	@ApiOperation(value = "更新个人信息")
	public PublicResultJosn updateMe(@RequestBody SystemUser user) {
		if (StringUtils.isBlank(user.getUid())) {
			throw new IllegalArgumentException("uid参数异常");
		}
		PublicResultJosn resultJosn = systemUserService.updateCurrentInfo(user);
		return resultJosn;
	}

	@PutMapping(value = "/current/pwd")
	@ApiOperation(value = "更新个人密码")
	public PublicResultJosn updatePassword(@RequestBody SystemUser user) {
		LoginUser loginUser = UserUtil.getCurrentUser();
		user.setUid(loginUser.getUid());
		user.setUsername(loginUser.getUsername());
		user.setPassword(passwordEncoder.encode(user.getPassword()));
		PublicResultJosn resultJosn = systemUserService.updateCurrentInfo(user);
		return resultJosn;
	}

	@PostMapping("/refresh")
	public PublicResultJosn refresh() {
		refreshUserInfo.refresh();
		return new PublicResultJosn(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), "更新成功");
	}

}
